<template>
	<view class="coupons">
		<n-loading></n-loading>
		<u-tabs :list="couponsTabs" :is-scroll="false" :current="current" @change="change" active-color="#FF5238" inactive-color="#666666" bar-width="120"></u-tabs>
		<view >
			<view class="coupon" v-if="current == 0">
				<view class="couponEach" v-for="(item,index) of couponList" :key="item.id">
					<view class="couponEachCont" :class="{boxShadow:cIndex == index && ruleShow}">
						<view class="couponEachSide">
							<view class="sideLeft">
								<view class="circle1"></view>
								<view class="circle2"></view>
								<view class="sideLeftCont">
									<!-- 1:商品减固金额2:商品折扣3:商品一口价4:订单减固定金额5:订单折扣6:订单一口价 -->
									<view class="amount" v-if="item.result_type==1 || item.result_type==3 || item.result_type==4 || item.result_type==6"><text>¥</text>{{item.condition}}</view>
									<view class="amount" v-if="item.result_type==2 || item.result_type==5">{{item.result}}<text>折</text></view>
									<view class="tips" v-if="item.result_type==1 || item.result_type==3 || item.result_type==4 || item.result_type==6">商品满减</view>
									<view class="tips" v-if="item.result_type==2 || item.result_type==5">商品折扣</view>
									
								</view>
							</view>
							<view class="sideRight">
								<view class="circle1"></view>
								<view class="circle2"></view>
								<view class="sideRightCont">
									<view class="store">
										<image :src="item.pic" mode=""></image>
										<view class="storeName ellipsis">{{item.store_name}}</view>
									</view>
									<view class="title ellipsis"><text>{{item.name}}</text></view>
									<view class="use">{{getdate(item.end_at,1)}}前使用</view>
									<view class="useRule" @tap="seeRule(index)">使用规则<u-icon name="arrow-down" color="#999999" size="12"></u-icon></view>
								</view>
							</view>
						</view>
					</view>
					<view class="couponBtn">立即使用</view>
					<view class="couponBtnEmpty" @tap="useNow(item.owner_id)"></view>
					
					<view class="ruleContent" v-if="ruleShow && cIndex == index" :data-index="index">
						<view class="ruleInfo">{{item.name}}</view>
					</view>
				</view>
			</view>
			<view class="coupon" v-if="current == 1">
				<view class="couponEach" v-for="(item,index) of couponList" :key="item.id">
					<view class="couponEachCont" :class="{boxShadow:cIndex == index && ruleShow}">
						<view class="couponEachSide">
							<view class="sideLeft">
								<view class="circle1"></view>
								<view class="circle2"></view>
								<view class="sideLeftCont sideLeftContEd">
									<view class="amount" v-if="item.result_type==1 || item.result_type==3 || item.result_type==4 || item.result_type==6"><text>¥</text>{{item.condition}}</view>
									<view class="amount" v-if="item.result_type==2 || item.result_type==5">{{item.result}}<text>折</text></view>
									<view class="tips" v-if="item.result_type==1 || item.result_type==3 || item.result_type==4 || item.result_type==6">商品满减</view>
									<view class="tips" v-if="item.result_type==2 || item.result_type==5">商品折扣</view>
									<!-- <view class="amount"><text>¥</text>{{item.conditions.result}}</view>
									<view class="tips">{{item.conditions.condition_type_name}}</view> -->
								</view>
							</view>
							<view class="sideRight">
								<view class="circle1"></view>
								<view class="circle2"></view>
								<view class="sideRightCont">
									<view class="store">
										<image :src="item.pic" mode=""></image>
										<view class="storeName ellipsis">{{item.store_name}}</view>
									</view>
									<view class="title ellipsis"><text>{{item.name}}</text></view>
									<view class="use">{{getdate(item.end_at,1)}}前使用</view>
									<view class="useRule" @tap="seeRule(index)">使用规则<u-icon name="arrow-down" color="#999999" size="12"></u-icon></view>
								</view>
							</view>
						</view>
					</view>
					<view class="couponBtn couponBtnEd">立即使用</view>
					<view class="couponBtnEmpty"></view>
					<!-- 已领取图标 -->
					<image class="claimedIcon" :src="item.status==3?'../../static/img/expired.svg':'../../static/img/used.svg'"></image>
					
					<view class="ruleContent" v-if="ruleShow && cIndex == index" :data-index="index">
						<view class="ruleInfo">{{item.conditions.result_type_name}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="nodata" v-if="couponList.length==0">
			<image src="../../static/svgIcon/noCoupon.svg" mode=""></image>
			<view class="tips">暂无优惠券~</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				offset:1,
				status:0,
				couponList:[],
				couponUsed:[],
				couponReady:null,
				couponsTabs: [{
					name: '待使用'
				}, {
					name: '已使用/已过期'
				}],
				current: 0,
				ruleShow:false,
				cIndex:''
			}
		},
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			// this.getCoupon()
			if (!uni.getStorageSync('userInfo')) {
				//用户登录
				this.login();
			} else {
				//调用默认启动方法
				this.loadData();
			}
		},
		onPullDownRefresh() {
			// this.couponList = [];
			// this.couponUsed = [];
			// this.getCoupon();
			this.offset=1
			this.status=0
			this.loadData()
			setTimeout(function () {
				uni.stopPullDownRefresh();  //停止下拉刷新动画
			}, 2000);
		},
		methods: {
			//---------------------------通用方法调用
			loadData() {
				this.get_cardData();//获取酒卡数据
			},
			
			//--------------------单表查询案例
			   async get_cardData() {
			    let url = '/api/user/listData'; //接口地址
			    let param = {};
			    param.table = 'promotion_coupon';//表名称
			    param.listType = 3; //分页获取商品数据 1单条  2全部  3分页
				param.offset=this.offset;// 页码数 默认1
				param.limit=10;//每页显示的数量 默认10
			    param.db = 1; //0获取缓存数据  1 获取非缓存数据  默认0
			    param.dbNum = ''; //缓存编号   API默认以MD5(param)为编号
				param.where={'a.status':this.status,'b.end_at':[this.status==1?'<':'>',uni.getStorageSync('userInfo').time]};//查询条件字段名称 无给个空值
			    param.field = 'a.status as use_status,b.*,c.result,c.result_type,c.content,c.content_type,c.condition,c.condition_type,d.name as store_name,d.image_id'; //联表查询返回记录合并记录
			    param.join =[
					['promotion b','a.promotion_id=b.id'],
					['promotion_condition c','b.id=c.promotion_id'],
					['store d','b.owner_id=d.owner_id'],
					] ; //联表查询返回记录合并记录
			    const res = await this.$fly.post(url, {
			     msg: JSON.stringify(param)
			    });
			    if (res.code == 200) {
					res.data.forEach(item=>{
						if(item.status==1 && item.end_at<uni.getStorageSync('userInfo').time){
							item.status=3
						}
					})
					
					this.couponList=res.data
					console.log(res.data);
			    } else {
					console.log(res);
			    }
			   },
			
			
			getCoupon(){
				this.apiget('/fun/coupon/user-coupon').then(res =>{
					if(res.data.status == 200 ) {
						this.couponList = res.data.data.coupon_list;
						this.couponUsed = res.data.data.used_list;
						this.couponReady = 1;
					}
				})
			},
			change(index) {
				this.current = index;
				if(index==0){
					this.status=0
				}else{
					this.status=1
				}
				this.offset=1
				this.couponList=[]
				this.loadData()
			},
			// 更多规则
			seeRule(index){
				this.ruleShow = !this.ruleShow;
				this.cIndex = index;
			},
			useNow(wId){
				if(wId=='1'){
					uni.reLaunch({
						url: '../index?barCur=0'
					});
				}else{
					uni.navigateTo({
						url: '../publicPraise/storeInfo?id='+wId
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.coupons{
		min-height: 100%;
		background-color: #F8F8FA;
	}
	
	.coupon {
		padding: 16rpx 32rpx;
	}
	.couponItme:last-child {
		border: none;
	}
// 新优惠券
	.couponEach{
		position: relative;
		margin-bottom: 16rpx;
		min-height: 224rpx;
		padding-top: 196rpx;
		
		.couponEachCont{
			position: absolute;
			left: 0;
			top: 0;
			z-index: 2;
			height: 224rpx;
			width: 100%;
			padding-right: 94rpx;
			box-sizing: border-box;
			
			.couponEachSide{
				display: flex;
				align-items: center;
				width: 100%;
				height: 100%;
				padding-right: 16rpx;
				background-color: #fff;
				border-radius: 16rpx;
				
				.sideLeftCont{
					text-align: center;
					height: 100%;
					padding: 18rpx;
					border-right: 2rpx dashed #ccc;
					color: #FF5238;
					
					.amount{
						font-size: 65rpx;
						line-height: 56rpx;
						
						text{
							font-size: 32rpx;
						}
					}
					
					.tips{
						font-size: 24rpx;
						line-height: 34rpx;
					}
				}
				
				.sideLeftContEd{
					color: #666;
				}
				
				.sideLeft{
					position: relative;
					padding: 48rpx 0;
					width: 160rpx;
					height: 100%;
					
					.circle1{
						position: absolute;
						top: -14rpx;
						right: -14rpx;
						width: 28rpx;
						height: 28rpx;
						background-color: #F8F8FA;
						border-radius: 50%;
					}
					.circle2{
						position: absolute;
						bottom: -14rpx;
						right: -14rpx;
						width: 28rpx;
						height: 28rpx;
						background-color: #F8F8FA;
						border-radius: 50%;
					}
				}
				
				.sideRight{
					flex: 1;
					width: 1px;
					position: relative;
					padding: 30rpx 0 30rpx 32rpx;
					height: 100%;
					
					.sideRightCont{
						
						.store{
							display: flex;
							margin-bottom: 9rpx;
							align-items: center;
							font-size: 24rpx;
							line-height: 34rpx;
							color: #333333;
							
							image{
								width: 32rpx;
								height: 32rpx;
								margin-right: 8rpx;
								border-radius: 50%;
							}
							.storeName{
								flex: 1;
								width: 1px;
							}
						}
						
						.title{
							font-size: 28rpx;
							line-height: 40rpx;
							color: #666666;
							
							text{
								color: #333333;
								font-weight: 700;
								margin-right: 16rpx;
							}
						}
						
						.use{
							margin-top: 8rpx;
							font-size: 24rpx;
							line-height: 34rpx;
							color: #666666;
						}
						
						.useRule{
							margin-top: 8rpx;
							font-size: 24rpx;
							line-height: 34rpx;
							color: #999999;
							
							.u-icon{
								margin-left: 10rpx;
							}
						}
					}
					
					.circle1{
						position: absolute;
						top: -14rpx;
						left: -14rpx;
						width: 28rpx;
						height: 28rpx;
						background-color: #F8F8FA;
						border-radius: 50%;
					}
					.circle2{
						position: absolute;
						bottom: -14rpx;
						left: -14rpx;
						width: 28rpx;
						height: 28rpx;
						background-color: #F8F8FA;
						border-radius: 50%;
					}
				}
			}
		}
		
		.boxShadow{
			box-shadow: 0px 4rpx 60rpx -2rpx rgba(0, 0, 0, 0.05);
		}
		
		.couponBtn{
			position: absolute;
			right: 0;
			top: 0;
			z-index: 1;
			width: 110rpx;
			height: 224rpx;
			padding: 20rpx 32rpx 20rpx 48rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			line-height: 36rpx;
			color: #FFFFFF;
			background: linear-gradient(90deg, #FF5238 33.5%, rgba(255, 82, 56, 0.7) 100%);
			box-shadow: 0px 4rpx 8rpx rgba(0, 0, 0, 0.02);
			border-radius: 0px 16rpx 16rpx 0px;
		}
		.couponBtnEd{
			background: linear-gradient(90deg, #999999 33.5%, rgba(153, 153, 153, 0.7) 100%);
			color: #fff;
		}
		
		.couponBtnEmpty{
			position: absolute;
			right: 0;
			top: 0;
			z-index: 3;
			width: 110rpx;
			height: 224rpx;
		}
		
		.claimedIcon{
			position: absolute;
			right: 110rpx;
			top: 112rpx;
			z-index: 3;
			width: 80rpx;
			height: 80rpx;
		}
		
		.ruleContent{
			padding: 50rpx 48rpx 22rpx;
			background: #FFFFFF;
			border-radius: 0px 0px 16rpx 16rpx;
			
			.ruleInfo{
				position: relative;
				padding-left: 15px;
				font-size: 12px;
				line-height: 24px;
				color: #333;
			}
			.ruleInfo:after{
				position: absolute;
				content: '';
				top: 50%;
				left: 14rpx;
				width: 4rpx;
				height: 4rpx;
				margin-top: -2rpx;
				background-color: #333;
			}
		}
	}
</style>
